﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using Hids.Enums;

namespace Hids.Classes
{
    /// <summary>
    /// This class holds the methods and properties that will be used to contact the user when an attack occurs
    /// </summary>
    public class Notifications
    {
        #region Private Properties

        /// <summary>
        /// What email address the notification emails should be sent from
        /// </summary>
        private static string _senderEmail = "Davjd@davjd.com";

        #endregion

        #region Public Properties

        /// <summary>
        /// This list defines who should recieve an email when a notification is sent
        /// </summary>
        public static List<string> RecipientEmails = new List<string>();

        /// <summary>
        /// The SMTP server to send email through
        /// </summary>
        public static string SmtpServer = "";

        #endregion

        #region Static Methods

        /// <summary>
        /// Sends a notification email to the recipient emails given in the static property
        /// </summary>
        /// <param name="body">What the notification email body should say</param>
        /// <returns>Whether or not the email notification was successfully sent</returns>
        public static bool SendEmail(string body)
        {
            if (RecipientEmails.Count == 0 || SmtpServer == "") //if no smtp server is defined, return false because we can't send an email
                return false;

            MailMessage newMessage = new MailMessage();
            foreach (string reciepent in RecipientEmails)
                newMessage.To.Add(reciepent);  //add the reciepents
            newMessage.From = new MailAddress(_senderEmail);
            newMessage.Body = body;
            newMessage.Subject = "Potential Attack Detected on " + Environment.MachineName;

            try
            {
                SmtpClient smtpSender = new SmtpClient(SmtpServer);
                smtpSender.Send(newMessage); //send the message
                return true;
            }
            catch (Exception except)//catch any errors when sending the email
            {
                Logs.WriteMessageToLog("Unable To Send Notification Email through " + SmtpServer + ".\nError Message:  " + except.Message);
                return false;  //if the mail message wasn't sent correctly, return false;
            }
        }

        #endregion
    }
}
